package com.friday.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.friday.dao.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class InvoiceController {

    @Autowired
    private InvoiceDao invoiceDao;
    @Autowired
    private OrderDao orderDao;
    @Autowired
    private DeliveryDao deliveryDao;
    @Autowired
    private QuotationDao quotationDao;
    @Autowired
    private InquiryDao inquiryDao;

    @RequestMapping("/invoiceAdd")
    public JSONObject addInvoice(@RequestParam("id") int order_id){
        JSONObject result=new JSONObject();
        result.put("result","fail");
        JSONObject invoice=orderDao.getOrderById(order_id);
        invoice.put("operator_id",10007);
        invoice.put("order_id",order_id);
        int tag1=invoiceDao.addInvoice(invoice);
        if(tag1>0){
            int tag2=0;
            int invoice_id=invoice.getInteger("id");
            JSONArray invoicedtl=orderDao.getOrderDtlByOId(order_id);
            for(int i=0;i<invoicedtl.size();i++){
                invoicedtl.getJSONObject(i).put("invoice_id",invoice_id);
                tag2+=invoiceDao.addInvoicedtl(invoicedtl.getJSONObject(i));
            }
            if(tag2==invoicedtl.size()){
                result.put("result","success");
                result.put("invoice_id",invoice_id);
            }
        }
        return result;
    }

    @RequestMapping("/invoiceDetail")
    public JSONObject getInvoicedtlByOID(@RequestParam("order_id") int order_id){
        JSONObject invoice=invoiceDao.getInvoiceByOId(order_id);
       //加入订单信息
        JSONObject order=orderDao.getOrderById(order_id);
        invoice.put("order_id",order_id);
        invoice.put("order_status",order.getString("status"));
        invoice.put("order_date",order.getDate("order_date"));
        invoice.put("order_opname",order.getString("operator_name"));
        invoice.put("order_opid",order.getInteger("operator_id"));
        //加入送货单信息
        JSONObject delivery=deliveryDao.getDeliveryByOId(order_id);
        int delivery_id=delivery.getInteger("id");
        invoice.put("delivery_id",delivery_id);
        invoice.put("delivery_date",delivery.getDate("docum_date"));
        invoice.put("delivery_record",deliveryDao.getDeliRecordByDeli(delivery_id));
        invoice.put("deli_opname",delivery.getString("operator_name"));
        invoice.put("deli_opid",delivery.getInteger("operator_id"));
        //加入报价单信息
        JSONObject quotation=quotationDao.getQuotationForOrder(order_id);
        int quotation_id=quotation.getInteger("id");
        invoice.put("quotation_id",quotation_id);
        invoice.put("quotation_date",quotation.getDate("docum_date"));
        invoice.put("quo_opname",quotation.getString("operator_name"));
        invoice.put("quo_opid",quotation.getInteger("operator_id"));
        if(quotation.containsKey("inquiry_id"))
        {
            JSONObject inquiry=inquiryDao.getInquiryByQuo(quotation_id);
            invoice.put("inquiry_id",inquiry.getInteger("id"));
            invoice.put("inquiry_date",inquiry.getDate("docum_date"));
            invoice.put("inq_opname",inquiry.getString("agent_name"));
            invoice.put("inq_opid",inquiry.getInteger("agent_id"));
        }
        //加入询价单信息
        int invoice_id=invoice.getInteger("id");
        //加入发票详情
        invoice.put("invoicedtl",invoiceDao.getInvoicedtlById(invoice_id));
        return invoice;
    }

}
